<?php
namespace Common\Model;

/**
 * 微预定推送信息关联表
 * @modify wenhui 2017-03-16 创建
 * 
 */

Class HotelNewspushRelateModel extends \Common\Model\BaseModel
{
    protected $trueTableName = 'bk_hotel_newspush_relate';
    protected function _before_update(&$data, $options) {
    }
    /**
     * 添加数据
     * @modify wenhui 2017-03-16 创建
     * @param  array   $newsPushRelateToAdd   新增数据数组
     * @return boolean 执行结果
     */
    public function addPushNewsRelateInfo($hotelId, $newsPushRelateToAdd) {
        foreach ($newsPushRelateToAdd as $key => &$newsPushRelateItem) {
            $newsPushRelateItem['hotelentity_id'] = $hotelId;
        }
        unset($newsPushRelateItem);

        return $this->addAll($newsPushRelateToAdd) ? true : false;
    }

    /**
     * 获取数据
     * @modify wenhui 2017-03-16 创建
     * @param  integer   $hotelId          客栈ID
     * @param  integer   $userId  用户id
     * @param  integer   $type  状态类型 0= 未读 1 =已读
     * @return array $data 执行结果
     */
    public function getNewsRelateByHotelAndUserId($hotelId = null, $userId = null, $type = null) {
        $option['isdelete'] = 0;

        if (is_array($hotelId)) {
            $hotelId = implode(',', $hotelId);
        }
        if (!is_null($hotelId)) {
            if (strpos($hotelId, ',') !== false) {
                $option['hotelentity_id'] = array('in', $hotelId);
            } else {
                $option['hotelentity_id'] = $hotelId;
            }
        }

        if (!is_null($userId)) {
            $option['adminlogin_id'] = $userId;
        }
        if (!is_null($type)) {
            $option['newstype'] = $type;
        }

        $data = $this->where($option)->select();

        return empty($data) ? array() : $data;
    }
   
    /**
     * 根据客栈id获取数据 
     * @modify wenhui 2017-03-16 创建
     * @param  array   $pushList   修改的数据
     * @return bool    
     */
    public function saveNews($pushList, $userId = null) {

        if (!empty($pushList['newsPushRelateId'])) {
            $option['id'] = $pushList['newsPushRelateId'];
        }
        if (!is_null($userId)) {
            $option['adminlogin_id'] = $userId;
        }

        if ($pushList['type'] == 1 || $pushList['type'] == 3) {

            $data['newstype'] = 1;
        } else if ($pushList['type'] == 2) {
            // 根据newsPushRelateId删除一个
            return $this->delete($option['id']) ? true : false;
        } else if ($pushList['type'] == 4) {
            // 根据userId全部删除
            return $this->where($option)->delete() ? true : false;
        }

        $result = $this->where($option)->save($data);
        return $result === false ? false : true;
    }
  

    /**
     * 记录redis数据
     * @modify wenhui 2017-05-09          创建
     * @param  userId   用户ID
     * @return boolean 执行结果
     */
    public function saveNewsRedis($userId) {
        $this->redisUtil = new \Org\Util\RedisHelper(null, null, 'meizhu');
        $this->redisUtil->db(C('REDIS_ORDER_PUSH_DB'));
        $addResult = $this->redisUtil->redis->sAdd('userIds', $userId);
    }
}
